package com.market.cash.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.market.cash.R
import com.market.cash.databinding.CashBindCardOtherDialogBinding
import com.market.cash.pro.ProDialog
import com.market.cash.utils.CashRootImageUtil
import com.market.cash.utils.event.CashRootEventHelper
import com.market.cash.utils.event.CashRootEventType
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/23 13:45
 * @Annotation：绑卡换卡dialog
 */
class CashRootBindCardOtherDialog: ProDialog {
    private lateinit var binding: CashBindCardOtherDialogBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = CashBindCardOtherDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {

    }

    override fun initListener() {
        setOnClickListener(binding.imgClose)
        setOnClickListener(binding.btnCancel)
        setOnClickListener(binding.btnConfirm) //两个按钮 又侧按钮
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        when(v!!.id){
            R.id.btnCancel -> {
                CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_USER_ACCOUNT_BIND_OTHER_CANCEL,eventRemark)
            }
            R.id.btnConfirm -> {
                CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_USER_ACCOUNT_BIND_OTHER_CONFIRM,eventRemark)
            }
        }
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    /**
     * 设置账户信息
     */
    fun setBindAccountInfo(info: JSONObject){
        binding.accountLayout.textIndex.text = String.format(context.getString(R.string.step_bind_card_index),1)
        CashRootImageUtil.instance.loadImageUrl(context,binding.accountLayout.accountLayout.imgLogo,info.optString("logo"), R.mipmap.cash_holder)
        binding.accountLayout.accountLayout.textTitle.text = info.optString("desc")
        binding.accountLayout.accountLayout.textMobile.text = info.optString("walletMobile")
        //不显示删除
        binding.accountLayout.accountLayout.imgDelete.visibility = View.GONE
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_USER_ACCOUNT_BIND_OTHER_TIPS_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_USER_ACCOUNT_BIND_OTHER_TIPS_DISMISS,eventRemark)
    }
}